package com.example.cet4try.MyFragment;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.cet4try.R;
import com.example.cet4try.activity.ActivityAdmin;
import com.example.cet4try.activity.ActivityFeedback;
import com.example.cet4try.activity.ActivitySelectDataBase;
import com.example.cet4try.activity.ActivitySetting;
import com.example.cet4try.activity.ActivityWordList;
import com.example.cet4try.activity.ActivityLogin;
import com.example.cet4try.activity.ActivityVocabulary;
import com.example.cet4try.adapter.PageListAdapter;

public class MyFragment3 extends Fragment {

    private SharedPreferences preferences;
    private Activity activity;
    private int admin = 0;
    private boolean isYouke = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.page3,container,false);

        initView();

        TextView user_name = v.findViewById(R.id.user_name);
        String nickname = preferences.getString("nickname","");
        String username = preferences.getString("username","");
        user_name.setText(nickname);

        if (username.equals("admin")){
            admin = 1;
        }
        if (username.equals("yk"))
            isYouke = true;

        String[] list;
        if (admin == 1){
            list = new String[]{"管理员功能","选择词库","词汇量","错题记录","我的收藏","意见反馈","设置"};
        }
        else {
            list = new String[]{"选择词库","词汇量","错题记录","我的收藏","意见反馈","设置"};
        }

        ListView ls_setting = v.findViewById(R.id.ls_setting);
        PageListAdapter setting = new PageListAdapter(list,activity);
        ls_setting.setAdapter(setting);
        ls_setting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = null;
                switch (position-admin){
                    case -1:
                        intent = new Intent(activity, ActivityAdmin.class);
                        break;
                    case 0:
                        intent = new Intent(activity, ActivitySelectDataBase.class);
                        break;
                    case 1:
                        intent = new Intent(activity, ActivityVocabulary.class);
                        break;
                    case 2:
                        intent = new Intent(activity, ActivityWordList.class);
                        intent.putExtra("type","wrong");
                        break;
                    case 3:
                        intent = new Intent(activity, ActivityWordList.class);
                        intent.putExtra("type","collect");
                        break;
                    case 4:
                        intent = new Intent(activity, ActivityFeedback.class);
                        break;
                    case 5:
                        intent = new Intent(activity, ActivitySetting.class);
                        break;
                    default:break;
                }
                if (intent != null){
                    if (!isYouke || position - admin == 4 || position - admin == 1)
                        startActivity(intent);
                    else
                        Toast.makeText(getContext(),"请先登陆",Toast.LENGTH_SHORT).show();
                }
            }
        });

        Button btn_exit = v.findViewById(R.id.btn_exit);
        btn_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString("username","");
                editor.putString("nickname","");
                editor.apply();
                startActivity(new Intent(activity, ActivityLogin.class));
                activity.finish();
            }
        });

        return v;
    }

    private void initView(){
        activity = getActivity();
        preferences = activity.getSharedPreferences("App_Shared_Data", 0);
    }


}
